package com.glory.gloryquickui.view

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.Button
import androidx.core.content.res.ResourcesCompat
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean

/**
 *按钮组件
 */
class ButtonController(context: Context?) : ViewController<FieldBean?>(context) {
    private var btnMain: Button? = null
    override fun resLayoutId(): Int {
        return R.layout.layout_button
    }

    override fun onBindView(data: FieldBean?) {
        btnMain!!.text = data!!.slot.default
        //设置禁用
        btnMain?.isEnabled = !data.disabled
        //设置按钮类型
        when(data.type){
            "primary" -> btnMain?.setBackgroundResource(R.drawable.selector_blue_fill_corner12)
            "success" -> btnMain?.setBackgroundResource(R.drawable.selector_green_fill_corner12)
            "warning" -> btnMain?.setBackgroundResource(R.drawable.selector_orange_fill_corner12)
            "danger" -> btnMain?.setBackgroundResource(R.drawable.selector_red_fill_corner12)
            "info" -> btnMain?.setBackgroundResource(R.drawable.selector_gray_fill_corner12)
            "text" -> {
                btnMain?.setBackgroundResource(0)
                btnMain?.setTextColor(Color.BLACK)
            }
        }
        //设置按钮图标
        if ( data.icon.isNotEmpty()) {
            val searchDrawable =
                context?.resources?.let { ResourcesCompat.getDrawable(it, R.drawable.search, null) }
            searchDrawable?.setBounds(
                0,
                0,
                searchDrawable.intrinsicWidth,
                searchDrawable.intrinsicHeight
            )
            btnMain?.compoundDrawablePadding = 8
            btnMain?.setCompoundDrawables(searchDrawable, null, null, null)

        }


    }

    override fun onCreatedView(view: View?) {
        btnMain = view?.findViewById(R.id.btnMain)
    }

    override fun masterPart(): View? {
        return btnMain
    }
}